package com.example.rwq_test.threadLocal.integer.excute;

import com.example.rwq_test.threadLocal.MyThreadLocalUtil;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * TODO note
 *
 * @author renweiqiang
 * @version 1.0.0
 * @createTime 2024/4/12
 */
public class MyIntegerThreadFactory implements ThreadFactory {
    private static final AtomicInteger THREAD_NUM = new AtomicInteger(0);

    @Override
    public Thread newThread(Runnable r) {
        Thread result = new Thread(r);
        // 设置线程名字
        result.setName("Thread-" + THREAD_NUM.getAndIncrement());
        // 设置每个线程的Integer类型数据
        ThreadLocal<Integer> integerThreadLocal = MyThreadLocalUtil.getIntegerThreadLocal();
        integerThreadLocal.set(THREAD_NUM.get());
        // 打印ThreadLocal的integer值
        System.out.println("integerThreadLocal value: " + integerThreadLocal.get());
        return result;
    }
}
